//
//
//  伺服参数设置界面
//
//


import QtQuick
import QtQuick.Controls.Material

Item {
    width: rootWidth
    height: rootHeight /*- 150*/

    A_NumberKeyboard{
        id: servoSettingKeyboard
        isDirectOverwrite: false
    }

    Component.onCompleted: {
        Cpp_PLC.QML_CurrentPage = 13;
        // Cpp_PLC.write_Modbus_RTU_Hold_16(48,"1")
    }


    Rectangle{
        width: rootWidth
        height: 1875
        color: "transparent"
        border.width: 1

        Item{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 50
            width: 700
            height: 100

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                label_Data: qsTr("每圈脉冲数")
            }

            // A_TextInput{
            //     anchors.left: parent.left
            //     anchors.leftMargin: 350
            //     anchors.verticalCenter: parent.verticalCenter
            //     inputWidth: 250
            //     showData: Cpp_PLC.PulseNumber_PerRound

            //     onInputFinished: {
            //         // console.log(inputData)
            //         // Cpp_PLC.write_Modbus_RTU_Hold_32(200,inputData)
            //         if( Number(inputData) < 0 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(200,"0")
            //         }
            //         else if( Number(inputData) > 100000 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(200,"100000")
            //         }
            //         else
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(200,inputData)
            //         }
            //     }
            // }

            A_NumberInput{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                inputWidth: 250
                text: Cpp_PLC.PulseNumber_PerRound
                minValue: 0
                maxValue: 100000
                keyboard: servoSettingKeyboard

                onEditingFinished: function(value)
                {
                    Cpp_PLC.write_Modbus_RTU_Hold_32(200,value)
                }

            }


        }

        Item{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 150
            width: 700
            height: 100

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                label_Data: qsTr("每圈位移量")
            }

            // A_TextInput{
            //     anchors.left: parent.left
            //     anchors.leftMargin: 350
            //     anchors.verticalCenter: parent.verticalCenter
            //     inputWidth: 250
            //     showData: Cpp_PLC.DistanceNumber_PerRound

            //     onInputFinished: {
            //         // console.log(inputData)
            //         // Cpp_PLC.write_Modbus_RTU_Hold_32(202,inputData)
            //         if( Number(inputData) < 0 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(202,"0")
            //         }
            //         else if( Number(inputData) > 100000 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(202,"100000")
            //         }
            //         else
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(202,inputData)
            //         }
            //     }
            // }


            A_NumberInput{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                inputWidth: 250
                text: Cpp_PLC.DistanceNumber_PerRound
                minValue: 0
                maxValue: 100000
                keyboard: servoSettingKeyboard

                onEditingFinished: function(value)
                {
                    Cpp_PLC.write_Modbus_RTU_Hold_32(202,value)
                }

            }

        }

        Item{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 250
            width: 700
            height: 100

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                label_Data: qsTr("最大切割长度")
            }

            // A_TextInput{
            //     anchors.left: parent.left
            //     anchors.leftMargin: 350
            //     anchors.verticalCenter: parent.verticalCenter
            //     inputWidth: 250
            //     showData: Cpp_PLC.MaxCutLength

            //     onInputFinished: {
            //         // console.log(inputData)
            //         // Cpp_PLC.write_Modbus_RTU_Hold_32(216,inputData)
            //         if( Number(inputData) < 0 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(216,"0")
            //         }
            //         else if( Number(inputData) > 100000 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(216,"100000")
            //         }
            //         else
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(216,inputData)
            //         }
            //     }
            // }

            A_NumberInput{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                inputWidth: 250
                text: Cpp_PLC.MaxCutLength
                minValue: 0
                maxValue: 100000
                keyboard: servoSettingKeyboard

                onEditingFinished: function(value)
                {
                    Cpp_PLC.write_Modbus_RTU_Hold_32(216,value)
                }

            }

        }

        Item{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 350
            width: 700
            height: 100

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                label_Data: qsTr("反向软限位")
            }

            // A_TextInput{
            //     anchors.left: parent.left
            //     anchors.leftMargin: 350
            //     anchors.verticalCenter: parent.verticalCenter
            //     inputWidth: 250
            //     showData: Cpp_PLC.Reverse_Soft_Limit

            //     onInputFinished: {
            //         // console.log(inputData)
            //         // Cpp_PLC.write_Modbus_RTU_Hold_32(218,inputData)
            //         if( Number(inputData) < -999999 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(218,"-999999")
            //         }
            //         else if( Number(inputData) > 100000 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(218,"100000")
            //         }
            //         else
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(218,inputData)
            //         }
            //     }
            // }

            A_NumberInput{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                inputWidth: 250
                text: Cpp_PLC.Reverse_Soft_Limit
                minValue: -999999
                maxValue: 100000
                keyboard: servoSettingKeyboard

                onEditingFinished: function(value)
                {
                    Cpp_PLC.write_Modbus_RTU_Hold_32(218,value)
                }

            }

        }

        Item{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 450
            width: 700
            height: 100

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                label_Data: qsTr("找零点高速")
            }

            // A_TextInput{
            //     anchors.left: parent.left
            //     anchors.leftMargin: 350
            //     anchors.verticalCenter: parent.verticalCenter
            //     inputWidth: 250
            //     showData: Cpp_PLC.FindZero_HighSpeed

            //     onInputFinished: {
            //         // console.log(inputData)
            //         // Cpp_PLC.write_Modbus_RTU_Hold_32(220,inputData)
            //         if( Number(inputData) < 0 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(220,"0")
            //         }
            //         else if( Number(inputData) > 100000 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(220,"100000")
            //         }
            //         else
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(220,inputData)
            //         }
            //     }
            // }


            A_NumberInput{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                inputWidth: 250
                text: Cpp_PLC.FindZero_HighSpeed
                minValue: 0
                maxValue: 100000
                keyboard: servoSettingKeyboard

                onEditingFinished: function(value)
                {
                    Cpp_PLC.write_Modbus_RTU_Hold_32(220,value)
                }

            }

        }

        Item{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 550
            width: 700
            height: 100

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                label_Data: qsTr("找零点低速")
            }

            // A_TextInput{
            //     anchors.left: parent.left
            //     anchors.leftMargin: 350
            //     anchors.verticalCenter: parent.verticalCenter
            //     inputWidth: 250
            //     showData: Cpp_PLC.FindZero_LowSpeed

            //     onInputFinished: {
            //         // console.log(inputData)
            //         // Cpp_PLC.write_Modbus_RTU_Hold_32(222,inputData)
            //         if( Number(inputData) < 0 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(222,"0")
            //         }
            //         else if( Number(inputData) > 100000 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(222,"100000")
            //         }
            //         else
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(222,inputData)
            //         }
            //     }
            // }

            A_NumberInput{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                inputWidth: 250
                text: Cpp_PLC.FindZero_LowSpeed
                minValue: 0
                maxValue: 100000
                keyboard: servoSettingKeyboard

                onEditingFinished: function(value)
                {
                    Cpp_PLC.write_Modbus_RTU_Hold_32(222,value)
                }

            }

        }

        Item{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 650
            width: 700
            height: 100

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                label_Data: qsTr("加减速时间")
            }

            // A_TextInput{
            //     anchors.left: parent.left
            //     anchors.leftMargin: 350
            //     anchors.verticalCenter: parent.verticalCenter
            //     inputWidth: 250
            //     showData: Cpp_PLC.TimeOf_ACC_DEC

            //     onInputFinished: {
            //         // console.log(inputData)
            //         // Cpp_PLC.write_Modbus_RTU_Hold_16(208,inputData)
            //         if( Number(inputData) < 0 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_16(208,"0")
            //         }
            //         else if( Number(inputData) > 2000 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_16(208,"2000")
            //         }
            //         else
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_16(208,inputData)
            //         }
            //     }
            // }


            A_NumberInput{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                inputWidth: 250
                text: Cpp_PLC.TimeOf_ACC_DEC
                minValue: 0
                maxValue: 2000
                keyboard: servoSettingKeyboard

                onEditingFinished: function(value)
                {
                    Cpp_PLC.write_Modbus_RTU_Hold_16(208,value)
                }

            }

        }

        Item{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 750
            width: 700
            height: 100

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                label_Data: qsTr("刹车打开时间")
            }

            // A_TextInput{
            //     anchors.left: parent.left
            //     anchors.leftMargin: 350
            //     anchors.verticalCenter: parent.verticalCenter
            //     inputWidth: 250
            //     showData: Cpp_PLC.Brake_OpenTime

            //     onInputFinished: {
            //         // console.log(inputData)
            //         if( Number(inputData) < 0 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(1278,"0")
            //         }
            //         else if( Number(inputData) > 2 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(1278,"2")
            //         }
            //         else
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_32(1278,inputData)
            //         }

            //     }
            // }


            A_NumberInput{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                inputWidth: 250
                text: Cpp_PLC.Brake_OpenTime
                minValue: 0
                maxValue: 2
                keyboard: servoSettingKeyboard

                onEditingFinished: function(value)
                {
                    Cpp_PLC.write_Modbus_RTU_Hold_32(1278,value)
                }

            }

        }

        Item{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 850
            width: 700
            height: 100

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                label_Data: qsTr("刹车关闭延时")
            }

            // A_TextInput{
            //     anchors.left: parent.left
            //     anchors.leftMargin: 350
            //     anchors.verticalCenter: parent.verticalCenter
            //     inputWidth: 250
            //     showData: Cpp_PLC.Brake_CloseTime

            //     onInputFinished: {
            //         // console.log(inputData)

            //         if( Number(inputData) < 0 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_16(602,"0")
            //         }
            //         else if( Number(inputData) > 20 )
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_16(602,"20")
            //         }
            //         else
            //         {
            //             Cpp_PLC.write_Modbus_RTU_Hold_16(602,inputData)
            //         }
            //     }
            // }

            A_NumberInput{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                inputWidth: 250
                text: Cpp_PLC.Brake_CloseTime
                minValue: 0
                maxValue: 20
                keyboard: servoSettingKeyboard

                onEditingFinished: function(value)
                {
                    Cpp_PLC.write_Modbus_RTU_Hold_16(602,value)
                }

            }


        }


        Item{
            anchors.top:parent.top
            anchors.topMargin: 980
            anchors.horizontalCenter: parent.horizontalCenter
            width: 700
            height: 150

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                visible: Cpp_PLC.ServoType
                label_Data: qsTr("长按 2s ,坐标清零")
                label_FontColor: "red"
            }

            A_Button{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                visible: Cpp_PLC.ServoType
                contentText_Data: qsTr("设机械零点")
                // contentText_Fontsize: 40
                buttonWidth: 250

                onButtonPressedAndHold: {
                    // console.log("触发 坐标清零")
                    Cpp_PLC.write_Modbus_RTU_Coils(147,"1")
                }

                onButtonPressed: {
                    Cpp_PLC.write_Modbus_RTU_Coils(26,"1")
                }

                onButtonReleased: {
                    Cpp_PLC.write_Modbus_RTU_Coils(26,"0")
                }

            }


        }

        Item{
            anchors.top:parent.top
            anchors.topMargin: 1130
            anchors.horizontalCenter: parent.horizontalCenter
            width: 700
            height: 150

            A_Label{
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.verticalCenter: parent.verticalCenter
                labelBack: "black"
                labelBorderColor: "white"
                label_FontColor: "aqua"
                label_Data: Cpp_PLC.Encoder_Value
            }

            A_Button{
                anchors.left: parent.left
                anchors.leftMargin: 350
                anchors.verticalCenter: parent.verticalCenter
                contentText_Data: qsTr("读取电机位置")
                // contentText_Fontsize: 40
                buttonWidth: 250

                // onButtonClicked: {
                //     console.log("触发 读取电机位置")
                // }
                onButtonPressed: {
                    Cpp_PLC.write_Modbus_RTU_Coils(53,"1")
                }

                onButtonReleased: {
                    Cpp_PLC.write_Modbus_RTU_Coils(53,"0")
                }

            }


        }





    }



}
